package org.wanho.rent.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.wanho.rent.entity.LeadingPeriod;
import org.wanho.rent.entity.OnlineApply;
import org.wanho.rent.entity.SysUser;
import org.wanho.rent.util.PageInfo;
import org.wanho.rent.util.ResponseUtil;

/**
 * 在线申请控制器
 * @author student
 *
 */
@WebServlet(urlPatterns = "*.apply")
public class OnlineApplyController extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println(request.getServletPath() + "###");
		String servletPath = request.getServletPath();
		if (servletPath.equals("/addApply.apply")) {
			addApply(request, response);
			System.out.println("-========+/apply");
		} else if (servletPath.equals("/selectByProductId.apply")) {
			selectByProductId(request, response);
		}

	}

	/**
	 * 根据产品id查询申请(分页)
	 * @param request
	 * @param response
	 */
	private void selectByProductId(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String pageNo = request.getParameter("pageNo");
		String productId = request.getParameter("productId");
		System.out.println("pageNo=" + pageNo + " productId=" + productId);

		int page = Integer.parseInt(pageNo);

		List list = new ArrayList();

		list.add(new OnlineApply(1, 1, "张三", "32012119921111", 15.0, new Date()));
		list.add(new OnlineApply(2, 2, "李四", "32012119921112", 16.0, new Date()));
		list.add(new OnlineApply(3, 3, "王五", "320121199211143", 17.0, new Date()));
		// 返回数据到浏览器

		int pagesize = 2;
		if (page == 2) {
			ResponseUtil.doResponse("成功！",
					new PageInfo<OnlineApply>(3, page, 2, list.subList((page - 1) * pagesize, list.size())), true,
					response);

		} else {
			ResponseUtil.doResponse("成功！",
					new PageInfo<OnlineApply>(3, page, 2, list.subList((page - 1) * pagesize, pagesize * page)), true,
					response);
		}

	}

	/**
	 * 添加申请
	 * @param request
	 * @param response
	 */
	private void addApply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String productId = request.getParameter("productId");
		String productName = request.getParameter("productName");
		String applyPersonIDCard = request.getParameter("applyPersonIDCard");
		String applyPerson = request.getParameter("applyPerson");
		String applyNum = request.getParameter("applyNum");
		System.out.println("productId=" + productId + "## productName=" + productName);

		// 返回数据到浏览器
		ResponseUtil.doResponse("成功！", new SysUser("123", "344"), true, response);
	}

}
